﻿A = function(x) {

    this.x = x;
};
A.prototype.print = function () {

    alert(this.x);
}

Fingerbrisk.docRequest = {

    requests: {},

    type: {

        asyncParallelRequest: 1,
        pageLoadRequest: 2
    },

    onSuccess: function (response, request) {

        /* called on success of doc request with doc response */
        if (response.type == Fingerbrisk.docResponse.type.viewfull) {

            request.containerElement.innerHTML = response.document.view.html;

            for (var script in response.document.view.scripts) {

                Fingerbrisk.components.addComponent(Fingerbrisk.components.type.script.prototype, { "data": script });
            }

            for (var css in response.document.view.css) {

                Fingerbrisk.components.addComponent(Fingerbrisk.components.type.css.prototype, { "data": css });
            }

            for (var document in response.document.view.dependentDocuments) {

                Fingerbrisk.components.addComponent(Fingerbrisk.components.type.document,
                    {
                        "data": document,
                        "callback": Fingerbrisk.docResponse.onSuccess
                    });
            }
        }

        if (typeof this.successCallback == "function")
            this.successCallback(response);
    },

    onFailure: function (params) {

        if (!params) { }
    }
};

Fingerbrisk.docRequest.Request = function (params) {

    if (!params)
        params = {};

    /* optional */
    if (typeof params.type == "undefined")
        this.type = Fingerbrisk.docRequest.type.asyncParallelRequest;
    else
        this.type = params.type;

    /* optional */
    if (typeof params.containerElement == "undefined"
            || document.getElementById(params.containerElement) == null)          /* containerElement in which to load the html output, to be specified for request type != pageRequest (if the requested document is not view less) */
        this.containerElement = undefined;
    else
        this.containerElement = document.getElementById(params.containerElement);

    /* mandatory */
    if (typeof params.docPath == "undefined")                   /* xml docPath to load */
        this.docPath = undefined;
    else
        this.docPath = params.docPath;

    /* optional */
    if (typeof params.args == "undefined")                      /* {key: '_scb', value: '_check'} */
        this.args = [];
    else
        this.args = params.args;

    /* mandatory */
    if (typeof params.data == "undefined")                      /* {key: '_scb', value: '_check'} */
        this.data = null;
    else
        this.data = params.data;

    /* mandatory */
    if (typeof params.successCallback == "undefined")                      /* {key: '_scb', value: '_check'} */
        this.successCallback = null;
    else
        this.successCallback = params.successCallback;

    /* mandatory */
    if (typeof params.context == "undefined")                      /* {key: '_scb', value: '_check'} */
        this.context = null;
    else
        this.context = params.context;

    this.luid = Math.random();
};
Fingerbrisk.docRequest.Request.prototype.send = function () {

    if (this.type == Fingerbrisk.docRequest.type.pageLoadRequest) {

        /* load the document as a page */
        window.location.href = params.docPath;
    }
    else {

        Fingerbrisk.docRequest.requests[this.luid] = this;

        Fingerbrisk.components.addComponent(Fingerbrisk.components.type.script.prototype, {

            "data": "http://localhost:52646/DocumentBasedSite/Handler/DocHandler.ashx?_path="
                            + this.docPath
                            + "&_scb=Fingerbrisk.docResponse.onSuccess"
                            + "&_luid=" + this.luid
        });
    }
}